package com.vogue.family.ui.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.vogue.family.data.api.NetWorkManager;
import com.vogue.family.data.entity.BaseResponse;
import com.vogue.family.data.entity.Meeting;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

public class MainViewModel extends AndroidViewModel {

    private MutableLiveData<String> errorMsg;
    private MutableLiveData<List<Meeting>> list;
    protected Integer page=1;

    public MainViewModel(@NonNull Application application) {
        super(application);
        errorMsg=new MutableLiveData<>();
        list=new MutableLiveData<>();
    }

    public MutableLiveData<String> getErrorMsg() {
        return errorMsg;
    }

    public MutableLiveData<List<Meeting>> getList() {
        return list;
    }

    public void getMeeting(boolean refresh){
        if (refresh){
            //刷新参数初始化
            page=1;
        }else {
            //加载更多参数赋值
            page++;
        }
        NetWorkManager
                .getInstance()
                .getApiService()
                .getMeeting(page)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {})
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BaseResponse<List<Meeting>>>() {
                    @Override
                    public void onNext(BaseResponse<List<Meeting>> response) {
                        if (response.getCode()==0){
                            list.setValue(response.getData());
                        }else {
                            errorMsg.setValue(response.getMsg());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        errorMsg.setValue("错误信息："+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
